package com.live.video.controller;

import com.live.video.enums.ResultEnum;
import com.live.video.model.GiftPlan;
import com.live.video.model.User;
import com.live.video.server.UserServer;
import com.live.video.util.Result;
import com.live.video.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * Description:
 * User: godd
 * Date: 2018-01-03
 * Time: 22:49
 */
@RestController
@Slf4j
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserServer userServer;

    /**
     * 用户登录接口
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/3 23:47
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    Result login(User user) {
        user = userServer.login(user);
        if (user.getId() != null) {
            return ResultUtil.success(user);
        }
        return ResultUtil.error(ResultEnum.USER_PASSWORLD_ERROR);
    }

    /**
     * 用户登出操作
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/3 23:47
     */
    @RequestMapping(value = "logon", method = RequestMethod.POST)
    Result logon(User user) {
        return ResultUtil.success();
    }

    /**
     * 查询某一个手机号是否已经注册了
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/4 00:09
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    Result register(User user) {
        if(user==null){
            return ResultUtil.error(ResultEnum.ERRO);
        }

        boolean isExist = userServer.findByPhone(user.getPhone());
        return ResultUtil.success(isExist);
    }
    /**
     * 查询
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/4 00:09
     */
    @RequestMapping(value = "query", method = RequestMethod.GET)
    Result query(User user) {
        if(user==null){
            return ResultUtil.error(ResultEnum.ERRO);
        }

        if(user.getId()!=null){
            user = userServer.queryOne(user);
            return ResultUtil.success(user);
        }else{
            List list = userServer.queryAll(user);
            return ResultUtil.success(list);
        }
    }
}
